package com.shengwei.truck.driver.view.selfview;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatEditText;

import com.shengwei.truck.driver.R;
import com.shengwei.truck.driver.utils.CommonUtil;
import com.shengwei.truck.driver.utils.MyToast;

import java.lang.ref.WeakReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 自定义EditText：可过滤掉表情符号；输入长度超过最大限制时自动弹出提示
 */
public class MyNoEmojEditText extends AppCompatEditText {
    private Context mContext;

    public MyNoEmojEditText(Context context) {
        super(context);
        this.setFilters(new InputFilter[]{CommonUtil.newFilter(context)});
    }

    public MyNoEmojEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        final TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.MyEditText);
        int maxLength = attributes.getInteger(R.styleable.MyEditText_maxLength, 25);
        int editType = attributes.getInteger(R.styleable.MyEditText_editType, 0);
        this.setFilters(new InputFilter[]{new MyLengthFilter(mContext, maxLength, editType)});
    }

    /**
     * This filter will constrain edits not to make the length of the text
     * greater than the specified length.
     */
    public static class MyLengthFilter implements InputFilter {
        private int mMaxLength;
        Toast toast;
        String strEditType;
        private int mEditType = -1;
        Context mContext;

        public MyLengthFilter(Context context, int max, int editType) {
            mMaxLength = max;
            mEditType = editType;
            mContext = context;

            switch (mEditType) {
                case 0:
                    strEditType = "密码";
                    break;
                case 1:
                    strEditType = "姓名";
                    break;
                case 2:
                    strEditType = "姓";
                    break;
                case 3:
                    strEditType = "名";
                    break;
                case 4:
                    strEditType = "地址";
                    break;
                case 5:
                    strEditType = "手机号";
                    break;
                case 6:
                    strEditType = "设备号";
                    break;
                case 7:
                    strEditType = "电话号码";
                    break;
                case 8:
                    strEditType = "群名称";
                    break;
                case 9:
                    strEditType = "围栏名称";
                    break;
                case 10:
                    strEditType = "短信码";
                    break;
                case 11:
                    strEditType = "用户名";
                    break;
                default:
                    strEditType = "输入";
            }
        }

        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

            int keep = mMaxLength - (dest.length() - (dend - dstart));

            if (keep < (end - start)) {
                String tip = strEditType + "不能超过" + mMaxLength + "个字符";
                MyToast.showToastCenter(new WeakReference<>(mContext),tip,Toast.LENGTH_SHORT,true);
            }

            if (keep <= 0) {
                return "" ;
            } else if (keep >= end - start) {
                return filterEmoj(source);
            } else {
                keep += start;
                if (Character.isHighSurrogate(source.charAt(keep - 1))) {
                    --keep;
                    if (keep == start) {
                        return "";
                    }
                }
                return source.subSequence(start, keep);
            }
        }

        public CharSequence filterEmoj(CharSequence charSequence) {
            Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]|" +
                    "[\ud800\udc00-\udbff\udfff\ud800-\udfff]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
            Matcher matcher = emoji.matcher(charSequence);

            if (!matcher.find()) {
                if (mEditType == 11) {
                    if (!charSequence.equals("-")) {
                        return null;
                    }
                    else {
                        return "";
                    }
                }
                return null;
            } else {
                return "";
            }


        }
    }
}